package com.dick.base.session.util;

import com.dick.base.model.BaseDepartment;
import com.dick.base.security.BaseUserDetails;
import com.dick.base.session.dto.UserBaseInfo;
import com.dick.base.util.AuthorityUtil;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Optional;

public class SessionUtil {

    /**
     * 获取当前登录的用户
     * @return
     */
    public static UserBaseInfo currentUser() {
        BaseUserDetails userDetails = (BaseUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (userDetails == null) return null;
        return userDetails.getUser();
    }

    /**
     * 判断当前用户是否任职于巡视办综合处
     * @return
     */
    public static boolean isInInspectionOfficeGeneral() {
        return currentUser().getDepartmentTypes().contains(AuthorityUtil.DepartmentTypeInspectionOfficeGeneral);
    }

    /**
     * 判断当前用户是否是主任
     * @return
     */
    public static boolean isDirector() {
        return currentUser().getRoles().contains(AuthorityUtil.RoleDirector);
    }

    /**
     * 判断当前用户是否是副主任
     * @return
     */
    public static boolean isDeputyDirector() {
        return currentUser().getRoles().contains(AuthorityUtil.RoleDeputyDirector);
    }

    /**
     * 获取用户所属的巡视组id
     * @return
     */
    public static Integer myInspectionTeamId() {
        Optional<BaseDepartment> department = currentUser().getDepartments().stream().filter(
                d -> d.getDepartmentType().equals(AuthorityUtil.DepartmentTypeInspectionTeam)
        ).findAny();
        if (department.isPresent()) return department.get().getId();
        return 0;
    }
}
